<template>
  <el-dialog
    top="10vh"
    width="750px"
    title="添加/编辑"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'product',
      dialogValue: false,
      ruleForm: {
      },
      defaultForm: {
        id: undefined,
        email: '',
        product_name: '',
        best_use: '',
        price_min: '',
        price_max: '',
        age_min: '',
        age_max: '',
        url: '',
        shipping_option: ''
      },
      rules: {
        email: [
          { required: true, message: '请输入Email', trigger: 'blur' }
        ],
        product_name: [
          { required: true, message: '请输入Product Name', trigger: 'blur' }
        ],
        best_use: [
          { required: true, message: '请输入Best Use', trigger: 'blur' }
        ],
        price_min: [
          { required: true, message: '请输入Price Min', trigger: 'blur' }
        ],
        price_max: [
          { required: true, message: '请输入Price Max', trigger: 'blur' }
        ],
        age_min: [
          { required: true, message: '请输入Age Min', trigger: 'blur' }
        ],
        age_max: [
          { required: true, message: '请输入Age Max', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入URL', trigger: 'blur' }
        ],
        shipping_option: [
          { required: true, message: '请输入Shipping Option', trigger: 'blur' }
        ]
      },
      formList: [
        {
          key: 'email',
          name: 'input',
          label: 'Email',
          colSpan: 12
        },
        {
          key: 'product_name',
          name: 'input',
          label: 'Product Name',
          colSpan: 12
        },
        {
          key: 'best_use',
          name: 'input',
          label: 'Best Use',
          colSpan: 12,
          type: 'textarea'
        },
        {
          key: 'price_min',
          name: 'input',
          label: 'Price Min',
          colSpan: 12,
          type: 'number'
        },
        {
          key: 'price_max',
          name: 'input',
          label: 'Price Max',
          colSpan: 12,
          type: 'number'
        },
        {
          key: 'age_min',
          name: 'input',
          label: 'Age Min',
          colSpan: 12,
          type: 'number'
        },
        {
          key: 'age_max',
          name: 'input',
          label: 'Age Max',
          colSpan: 12,
          type: 'number'
        },
        {
          key: 'url',
          name: 'input',
          label: 'URL',
          colSpan: 12,

          type: 'textarea'
        },
        {
          key: 'shipping_option',
          name: 'input',
          label: 'Shipping Option',
          colSpan: 12,

          type: 'textarea'
        }
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      console.log('row',row)
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, { ...this.ruleForm }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
